$(function () {
    $("#sysUserGrid").Grid({
        url: '../sys/user/list',
        colModel: [
            {label: '用户ID', name: 'id', index: "id", key: true, hidden: true},
            {label: '用户名', name: 'userName', index: "user_id", width: 75},
            {label: '人员', name: 'personName', index: "person_id", width: 75},
            {label: '邮箱', name: 'email', width: 90},
//            {label: '手机号', name: 'mobile', width: 100},
            {label: '状态', name: 'status', width: 80, formatter: function (value) {
                return value === 0 ?
                    '<span class="label label-danger">禁用</span>' :
                    '<span class="label label-success">正常</span>';
                }
            },
            {
                label: '创建时间', name: 'createTime', index: "create_time", width: 80, formatter: function (value) {
                return transDate(value);
            }
            }]
    });
//    vm.getAppProject();
});

var vm = new Vue({
    el: '#sysUser',
    data: {
        q: {
            userName: null
        },
        showList: true,
        title: null,
        roleList: {},
        employeeList:[],
        user: {
            status: 1,
            deptName: '',
            employeeName: '',
            roleIdList: []
        },
        ruleValidate: {
            userName: [
                {required: true, message: '用户名不能为空', trigger: 'blur'}
            ],
            employeeName: [
                {required: true, message: '员工不能为空', trigger: 'blur'}
            ],
            email: [
                {type: 'email', message: '邮箱格式不正确', trigger: 'blur'}
            ]
        }
    },
    methods: {
        query: function () {
        	$("#sysUserGrid").jqGrid('setGridParam',{page:1});
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增(默认密码：888888)";
            vm.roleList = {};
            vm.user = {status: 1, roleIdList: [],userName:''};

            //获取角色信息
            this.getRoleList();
        },
        update: function () {
            var userId = getSelectedRow("#sysUserGrid");
            if (userId == null) {
                return;
            }

            vm.showList = false;
            vm.title = "修改";

            Ajax.request({
                url: "../sys/user/info/" + userId,
                async: true,
                successCallback: function (r) {
                    vm.user = r.user;
                    vm.employeeList = r.employeeList;
                    if(!vm.user.roleIdList){
                    	vm.user.roleIdList=[];
                    }
                    //获取角色信息
                    vm.getRoleList();
                }
            });

        },
        del: function () {
            var userIds = getSelectedRows("#sysUserGrid");
            if (userIds == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                Ajax.request({
                    url: "../sys/user/delete",
                    params: JSON.stringify(userIds),
                    contentType: "application/json",
                    type: 'POST',
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            });
        },
        delToken: function () {
        	confirm('确定要清除APP所有用户的Token记录？', function () {
        		Ajax.request({
        			url: "../sys/user/deleteToken",
        			contentType: "application/json",
        			type: 'POST',
        			successCallback: function () {
        				alert('操作成功', function (index) {
        					vm.reload();
        				});
        			}
        		});
        	});
        },
        changepassword: function () {
        	var userId = getSelectedRow("#sysUserGrid");
            if (userId == null) {
                return;
            }
        	
        	confirm('确定要初始化该用户的密码？', function () {
        		Ajax.request({
        			url: "../sys/user/initpassword?id="+userId,
        			contentType: "application/json",
        			type: 'GET',
        			successCallback: function () {
        				alert('操作成功', function (index) {
        					vm.reload();
        				});
        			}
        		});
        	});
        },
        saveOrUpdate: function (event) {
            var url = vm.user.id == null ? "../sys/user/save" : "../sys/user/update";
            Ajax.request({
                url: url,
                params: JSON.stringify(vm.user),
                contentType: "application/json",
                type: 'POST',
                successCallback: function () {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
            });
        },
        getRoleList: function () {
            Ajax.request({
                url: '../sys/role/select',
                async: true,
                successCallback: function (r) {
                    vm.roleList = r.list;
                }
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#sysUserGrid").jqGrid('getGridParam', 'page');
            $("#sysUserGrid").jqGrid('setGridParam', {
                postData: {'userName': vm.q.userName},
                page: 1
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
        },
        reloadSearch: function () {
        	$("#sysUserGrid").jqGrid('setGridParam',{page:1});
            vm.q = {
                userName: ''
            }
            vm.reload();
        },
        employeeGrid: function () {
            openWindow({
            	type: 2,
                title: "选择部门",
                area: ['80%', '60%'],
                content: ['../org/employeeSelect.html?condition=noSelect'],
                btn: ['确定', '取消'],
                btn1: function (index,layero) {
                	var selectRowData = window[layero.find('iframe')[0]['name']].getSelectedRowData('#orgEmployeeGrid');
                    vm.user.deptId = selectRowData.companyId;
                    vm.user.deptName = selectRowData.companyName;
                    vm.user.employeeId = selectRowData.id;
                    vm.user.employeeName = selectRowData.name;
                    vm.user.mobile = selectRowData.phone;
                    vm.user.userName = selectRowData.phone;

                    layer.close(index);
                }
            });
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        }
    }
});